﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Landsat8_LSTWindow
{
    public class CorrParam
    {
        public string t { get; set; }
        public string Lu { get; set; }
        public string Ld { get; set; }

        public static async Task<CorrParam> Init(string lc8MetaFile)
        {
            try
            {
                var meta = File.ReadAllText(lc8MetaFile);
                var lins = meta.Split('\n');
                Dictionary<string, string> dic = new Dictionary<string, string>();
                foreach (var lin in lins)
                {
                    var arr = lin.Split('=');
                    if (arr.Length == 2)
                    {
                        var key = arr[0].Trim(' ').Trim('\"');
                        var val = arr[1].Trim(' ').Trim('\"');
                        dic[key] = val;
                    }
                }

                var date = dic["DATE_ACQUIRED"].Split('-');
                var year = date[0]; var month = date[1]; var day = date[2];
                var time = dic["SCENE_CENTER_TIME"].Split(':');
                var hour = time[0]; var minute = time[1];

                var lat = (Math.Max(float.Parse(dic["CORNER_UL_LAT_PRODUCT"]), float.Parse(dic["CORNER_UR_LAT_PRODUCT"])) +
                    Math.Min(float.Parse(dic["CORNER_LL_LAT_PRODUCT"]), float.Parse(dic["CORNER_LR_LAT_PRODUCT"]))) / 2;
                var lon = (Math.Max(float.Parse(dic["CORNER_UL_LON_PRODUCT"]), float.Parse(dic["CORNER_LL_LON_PRODUCT"])) +
                Math.Max(float.Parse(dic["CORNER_UR_LON_PRODUCT"]), float.Parse(dic["CORNER_LR_LON_PRODUCT"]))) / 2;
                var para = new AtmCorrParam
                {
                    year = year,
                    month = month,
                    day = day,
                    hour = hour,
                    minute = minute,
                    thelat = lat,
                    thelong = lon,
                    user_email = "liuyu_rs_cug@outlook.com",
                    profile_option = 2,
                    L57_option = 8,
                    stdatm_option = lat > 40 ? 2 : 1
                };
                var client = new StandardHttpClient();
                var res = await client.PostFormAsync("https://atmcorr.gsfc.nasa.gov/cgi-bin/atm_corr.pl", para);
                var content = await res.Content.ReadAsStringAsync();
                var ss = new List<string> { "<br>" };
                var tt = content.Split(ss.ToArray(), StringSplitOptions.RemoveEmptyEntries);

                //Band average atmospheric transmission:    0.96
                //Effective bandpass upwelling radiance:    0.22 W / m ^ 2 / sr / um
                //Effective bandpass downwelling radiance:  0.42 W / m ^ 2 / sr / um
                string t = "", Lu = "", Ld = "";
                foreach (var lin in tt)
                {
                    if (lin.Contains("Band average atmospheric transmission:"))
                    {
                        t = lin.Split(':')[1].Trim(' ');
                    }
                    if (lin.Contains("Effective bandpass upwelling radiance:"))
                    {
                        Lu = lin.Split(':')[1].Trim(' ').Split(' ')[0].Trim(' ');
                    }
                    if (lin.Contains("Effective bandpass downwelling radiance:"))
                    {
                        Ld = lin.Split(':')[1].Trim(' ').Split(' ')[0].Trim(' ');
                    }
                }

                return new CorrParam
                {
                    t = t,
                    Lu = Lu,
                    Ld = Ld
                };
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return null;
        }
    }

}
